package com.lkd.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkd.common.VMSystem;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.VendingMachineService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 补货自动触发，生成订单
 */
@Slf4j
@Component
public class SupplyJob {
    @Autowired
    private VendingMachineService vendingMachineService;

    @XxlJob("supplyJobHandler")
    public ReturnT<String> supplyJobHandler(String param) {
        //1.查询所有在运行的售货机
        LambdaQueryWrapper<VendingMachineEntity> vendingMachineQueryWrapper = new LambdaQueryWrapper<>();
        vendingMachineQueryWrapper.eq(VendingMachineEntity::getVmStatus, VMSystem.VM_STATUS_RUNNING);
        List<VendingMachineEntity> vendingMachineEntityList = vendingMachineService.list(vendingMachineQueryWrapper);
        //2.计算是否缺货
        for (VendingMachineEntity vendingMachineEntity : vendingMachineEntityList) {
            vendingMachineService.computeAndSendMsg(vendingMachineEntity);
        }
        return ReturnT.SUCCESS;
    }
}
